<?php
class Usuarios extends Controller{

	function Usuarios()
	{
		parent::Controller();
		$this->load->database();
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->load->model('usuario');	
		$this->load->model('instituicao');
		$this->load->model('subprograma');
	}
	
	function index($subprograma_id = '')
	{
		$data['subprogramas'] = $this->subprograma->all();
		
		$data['usuarios'] = $this->usuario->getBySubprograma($subprograma_id);
			
		$data['subprograma_id'] = $subprograma_id;
		
		$this->layout->view('usuarios/index', $data);
	}
	
	function novo(){
		$data['instituicoes'] = $this->instituicao->all();
		$data['subprogramas'] = $this->subprograma->all();
		
 		$data['id']			    = "";
 		$data['instituicao_id'] = "";
 		$data['subprograma_id'] = "";
 		$data['perfil'] 		= "";
 		$data['nome']			= "";
 		$data['descricao']		= "";
 		$data['telefone'] 		= "";
 		$data['email']			= "";
 		$data['senha'] 			= "";
		
		$this->layout->view('usuarios/novo', $data);
	}
	
	function editar($id){
		$data['instituicoes'] = $this->instituicao->all();
		$data['subprogramas'] = $this->subprograma->all();
		
		$usuarios = $this->db->get_where('rel_usuario', array('id =' => $id))->result();
		$usuario = $usuarios[0];
		
		$data['id']			    = $usuario->id;
		$data['instituicao_id'] = $usuario->instituicao_id; 
		$data['subprograma_id'] = $usuario->subprograma_id; 
		$data['perfil'] 		= $usuario->perfil; 
		$data['nome']			= $usuario->nome; 
		$data['descricao']		= $usuario->descricao; 
		$data['telefone'] 		= $usuario->telefone; 
		$data['email']			= $usuario->email; 
		$data['senha'] 			= $usuario->senha; 
		
		$this->layout->view('usuarios/editar', $data);
	}
	
	function salvar() {
		$this->form_validation->set_rules('id','Id usuario','max_length[11]');			
		$this->form_validation->set_rules('instituicao_id','Instituicao Id','required|max_length[5]');	
		$this->form_validation->set_rules('subprograma_id','Subprograma Id','required|max_length[3]');			
		$this->form_validation->set_rules('perfil','perfil','required|max_length[1]');			
		$this->form_validation->set_rules('nome','nome','required|max_length[150]');			
		$this->form_validation->set_rules('descricao','Descricao','max_length[2000]');			
		$this->form_validation->set_rules('telefone','telefone','max_length[14]');			
		$this->form_validation->set_rules('email','email','required|valid_email|max_length[150]');			
		$this->form_validation->set_rules('senha','senha','required|max_length[32]');
			
		$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
	
		if ($this->form_validation->run() == FALSE) // validation hasn'\t been passed
		{
			$data['instituicoes'] = $this->instituicao->all();
			$data['subprogramas'] = $this->subprograma->all();
			
			$data['id']			    = '';
			$data['instituicao_id'] = '';
			$data['subprograma_id'] = '';
			$data['perfil'] 		= '';
			$data['nome']			= '';
			$data['descricao']		= '';
			$data['telefone'] 		= '';
			$data['email']			= '';
			$data['senha'] 			= '';
			
			$this->layout->view('usuarios/novo', $data);
		}
		else // passed validation proceed to post success logic
		{
		 	// build array for the model
			
			$form_data = array(
					       	'id' => set_value('id'),
					       	'instituicao_id' => set_value('instituicao_id'),
					       	'subprograma_id' => set_value('subprograma_id'),
					       	'perfil' => set_value('perfil'),
					       	'nome' => set_value('nome'),
					       	'descricao' => set_value('descricao'),
					       	'telefone' => set_value('telefone'),
					       	'email' => set_value('email'),
					       	'senha' => set_value('senha')
						);
					
			// run insert model to write data to db
			
			if ($this->usuario->save_form($form_data) == TRUE) // the information has therefore been successfully saved in the db
			{
				redirect(base_url() . 'index.php/usuarios');   // or whatever logic needs to occur
			}
			else
			{
				echo 'Ocorreu um erro ao salvar os seus dados, tente novamente mais tarde!';
				// Or whatever error handling is necessary
			}
		}
	}
}